package server

import (
	"account/drivers"
	"account/models"
)

// 获取所有停用的平台信息
func GetPlatBlackAllInfos() ([]models.Exchange, error) {
	var plats []models.Exchange
	if result := drivers.SqlClusterDb.
		Where(map[string]interface{}{"deleted": 1, "active": 0}).
		Or(map[string]interface{}{"deleted": 0, "active": 0}).
		Or(map[string]interface{}{"deleted": 1, "active": 1}).
		Or(map[string]interface{}{"deleted": "", "active": ""}).
		Find(&plats).
		GetErrors(); len(result) > 0 {
		return plats, result[0]
	}
	return plats, nil
}

// 获取所有的plat信息
func GetPlatAllInfos() ([]models.Exchange, error) {
	var plats []models.Exchange
	if result := drivers.SqlClusterDb.Where(map[string]interface{}{"deleted": 0, "active": 1}).Find(&plats).GetErrors(); len(result) > 0 {
		return plats, result[0]
	}
	return plats, nil
}

// 增加一条信息
func AddPlatInfo(plat models.Exchange) error {
	if err := drivers.SqlClusterDb.Create(&plat).Error; err != nil {
		return err
	}
	return nil
}

// 更新信息
func UpdatePlatInfo(id int, plat models.Exchange) error {

	if err := drivers.SqlClusterDb.Model(&models.Exchange{}).Where("id = ?", id).Update(
		map[string]interface{}{"name": plat.Name, "active": plat.Active, "deleted": plat.Deleted, "short_name": plat.ShortName}).Error; err != nil {
		return err
	}
	return nil
}

// 软删除 | active =0 or active =1 | deleted 无效的
func ActivePlatInfo(id int) error {
	if err := drivers.SqlClusterDb.Model(&models.Exchange{}).Where("id = ?", id).Update("active", 0).Error; err != nil {
		return err
	}
	return nil
}
